package com.chlitina.store.modules.cash.model;

import java.util.ArrayList;
import java.util.List;

import com.chlitina.store.common.utils.DataFormater;
import com.chlitina.store.modules.base.entity.StoreCare;
import com.chlitina.store.modules.cash.service.PaymentUility;
import com.chlitina.store.modules.employee.entity.Employee;

public class StoreCareCart {
	private CareDescription careDescription;
	private StoreCare storeCare;
	private Employee employee;
	private int count = 1;
	private int id;
	private boolean hasSet;
	private CarePayment payment;
	private float singlePrice;
	private float requiredTotal;
	private float realCostTotal;
	private float realCost;
	private List<String> details = new ArrayList<String>();

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public StoreCare getStoreCare() {
		return storeCare;
	}

	public void setStoreCare(StoreCare storeCare) {
		this.storeCare = storeCare;
	}

	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employeeCare) {
		this.employee = employeeCare;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public void updateDescription() {
		if (careDescription == null) {
			careDescription = new CareDescription();
		}
		careDescription.update(storeCare);
	}

	public CareDescription getCareDescription() {
		return careDescription;
	}

	public void calculatePayment(Member member) {
		updateDescription();
		updateRequiredPayment();
		CardPayment cardPayment = PaymentUility.findBestDurationCard(member
				.getAvaibleDurationCards(), getStoreCare().getId().longValue(),
				getRequiredTotal());
		payment = new CarePayment();
		if (cardPayment != null) {
			DurationCardPaymentDetail detail = new DurationCardPaymentDetail();
			detail.setRealCost(cardPayment.getRealCost());
			detail.setCardPayment(cardPayment);
			cardPayment.payMoney();
			payment.setDurattionCardPaymentDetail(detail);
			updateRealCost();
			payment.updateDescription();
			return;
		}
		TimesCardPayment timesCardPayment = (TimesCardPayment) PaymentUility
				.findBestTimesCard(member.getAvaibleTimesCards(), storeCare
						.getId().longValue());
		if (timesCardPayment != null) {
			TimesCardPaymentDetail detail = new TimesCardPaymentDetail();
			detail.setCurrentLeftMoney(timesCardPayment.getCurrentMoney());
			detail.setRealCost(timesCardPayment.getCostPertime() * count);
			detail.setTimes(count);
			detail.setCardPayment(timesCardPayment);
			timesCardPayment.payTimes(count);
			payment.setTimesCardPaymentDetail(detail);
			updateRealCost();
			payment.updateDescription();
			return;
		}
		AllCardPayment allCardPayment = PaymentUility.findBestAllCardForCare(
				member.getAvaibleAllCards(), getRequiredTotal());
		if (allCardPayment != null) {
			AllCardPaymentDetail detail = new AllCardPaymentDetail();
			detail.setCurrentLeftMoney(allCardPayment.getCurrentMoney());
			detail.setRealCost(allCardPayment.getRealCost());
			detail.setCardPayment(allCardPayment);
			allCardPayment.payMoney();
			payment.setAllCardPaymentDetail(detail);
			updateRealCost();
			payment.updateDescription();
			return;
		}
		payment.setCash(getRequiredTotal());
		updateRealCost();
		payment.updateDescription();
	}

	private void updateRealCost() {
		setRealCostTotal(0);
		realCost = 0;
		if (payment == null) {
			return;
		}
		setRealCostTotal(getPayment().getRealTotalCost());
		realCost = getRealCostTotal() / count;
	}

	private void updateRequiredPayment() {
		setRequiredTotal(0);
		setSinglePrice(storeCare.getSingle_price());
		setRequiredTotal(getSinglePrice() * count);
	}

	public void cancelPayment() {
		if (payment == null) {
			return;
		}
		payment = null;
	}

	public void setCareDescription(CareDescription careDescription) {
		this.careDescription = careDescription;
	}

	public boolean isHasSet() {
		return hasSet;
	}

	public void setHasSet(boolean hasSet) {
		this.hasSet = hasSet;
	}

	public CarePayment getPayment() {
		return payment;
	}

	public void setPayment(CarePayment payment) {
		this.payment = payment;
	}

	public float getRealCost() {
		return realCost;
	}

	public void setRealCost(float realCost) {
		this.realCost = realCost;
	}
	
	public List<String> getDetails() {
		return details;
	}

	public void updateDetails() {
		if (payment == null) {
			return;
		}
		details.clear();
		float retailPrice = storeCare.getSingle_price();
		details.add("做手工护理：【" + getProductName() + "】");
		details.add("" + retailPrice);
		details.add("" + count);
		if(retailPrice==0){
			details.add(DataFormater.format(0));
		}else{
			details.add(DataFormater.format((realCost  / retailPrice))  );
		}
		details.add("" + (realCost));
		details.add("" + realCostTotal);
		details.add(payment.getDescription());

	}

	private String getProductName() {
		if(storeCare.getCare_type()==StoreCare.TYPE_SELF){
			return storeCare.getStoreSelfCare().getName();
		}else{
			return storeCare.getCare().getName();
		}
	}

	public float getRealCostTotal() {
		return realCostTotal;
	}

	public void setRealCostTotal(float realCostTotal) {
		this.realCostTotal = realCostTotal;
	}

	public float getRequiredTotal() {
		return requiredTotal;
	}

	public void setRequiredTotal(float requiredTotal) {
		this.requiredTotal = requiredTotal;
	}

	public float getSinglePrice() {
		return singlePrice;
	}

	public void setSinglePrice(float singlePrice) {
		this.singlePrice = singlePrice;
	}
}
